package com.service.scheduler.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.StatefulJob;

import com.service.bean.device.Vehicle;
import com.service.cache.CacheManager;
import com.service.db.impl.DBSpeed;
import com.service.other.Logger;
/***
 * 1H 累计时间,更新数据库
 * @author Administrator
 *
 */
@SuppressWarnings("deprecation")
public class SubSpeedJob implements StatefulJob{

	public SubSpeedJob(){}
	@Override
	public void execute(JobExecutionContext context)throws JobExecutionException {
		try {
			JobKey jk = context.getJobDetail().getKey();
			String jobName = jk.getName();
			String jobGroup = jk.getGroup();
			Logger.println("启动任务 [" + jobGroup + "].[" + jobName + "]");
			long subSpeedTime = 0;
			for(Vehicle vehicle :CacheManager.instance().vehicleMap.values()){
				//取缓存中的时间
				long speedGap = CacheManager.instance().subSpeedMap.get(vehicle.device.deviceNo)==null?0:CacheManager.instance().subSpeedMap.get(vehicle.device.deviceNo);
				//取数据库中最新时间
				long time = DBSpeed.getSubSpeedTime(vehicle);
				subSpeedTime = speedGap+time;
				CacheManager.instance().subSpeedTimeMap.put(vehicle.device.deviceNo,subSpeedTime);
				if(0 == speedGap){
					continue ;
				}
				//取最新数据更新
				DBSpeed.updateSubSpeedTime(vehicle,subSpeedTime);
				//更新数据库之后，对缓存设置成0
				CacheManager.instance().subSpeedMap.put(vehicle.device.deviceNo,0L);
				
			}
		} catch (Exception e) {
			Logger.err(e);
		}

	}


}
